IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,
        [ProductVersion] nvarchar(32) NOT NULL,
        CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;
GO

BEGIN TRANSACTION;
GO

CREATE TABLE [ActivityTypeModels] (
    [ActivityTypeID] nvarchar(450) NOT NULL,
    [ActivityTypeName] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_ActivityTypeModels] PRIMARY KEY ([ActivityTypeID])
);
GO

CREATE TABLE [Managers] (
    [ManagerID] nvarchar(450) NOT NULL,
    [Name] nvarchar(max) NOT NULL,
    [Password] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_Managers] PRIMARY KEY ([ManagerID])
);
GO

CREATE TABLE [Organizations] (
    [OrganizationID] nvarchar(450) NOT NULL,
    [OrganizationTelephone] nvarchar(max) NOT NULL,
    [OrganizationName] nvarchar(max) NOT NULL,
    [OrganizationPassword] nvarchar(max) NOT NULL,
    [Province] nvarchar(max) NOT NULL,
    [City] nvarchar(max) NOT NULL,
    [Area] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_Organizations] PRIMARY KEY ([OrganizationID])
);
GO

CREATE TABLE [Voluntary] (
    [VoluntaryID] nvarchar(450) NOT NULL,
    [VoluntaryTelephone] nvarchar(max) NOT NULL,
    [VoluntaryName] nvarchar(max) NOT NULL,
    [VoluntaryPassword] nvarchar(max) NOT NULL,
    [Province] nvarchar(max) NOT NULL,
    [City] nvarchar(max) NOT NULL,
    [Area] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_Voluntary] PRIMARY KEY ([VoluntaryID])
);
GO

CREATE TABLE [VoluntaryActivities] (
    [ActivityID] nvarchar(450) NOT NULL,
    [ActivityName] nvarchar(max) NOT NULL,
    [ActivityTypeID] nvarchar(450) NOT NULL,
    [ActivityCreateTime] datetime2 NOT NULL,
    [ActivityCompleteTime] nvarchar(max) NOT NULL,
    [ActivityComplete] bit NOT NULL,
    [VoluntaryContent] nvarchar(max) NULL,
    [OrganizationID] nvarchar(450) NOT NULL,
    [Province] nvarchar(max) NOT NULL,
    [City] nvarchar(max) NOT NULL,
    [Area] nvarchar(max) NOT NULL,
    [DetailedAddress] nvarchar(max) NOT NULL,
    [IsDelete] bit NOT NULL,
    CONSTRAINT [PK_VoluntaryActivities] PRIMARY KEY ([ActivityID]),
    CONSTRAINT [FK_VoluntaryActivities_ActivityTypeModels_ActivityTypeID] FOREIGN KEY ([ActivityTypeID]) REFERENCES [ActivityTypeModels] ([ActivityTypeID]) ON DELETE CASCADE,
    CONSTRAINT [FK_VoluntaryActivities_Organizations_OrganizationID] FOREIGN KEY ([OrganizationID]) REFERENCES [Organizations] ([OrganizationID]) ON DELETE CASCADE
);
GO

CREATE TABLE [VAVRelationshipsTable] (
    [RelationID] nvarchar(450) NOT NULL,
    [VoluntaryID] nvarchar(450) NOT NULL,
    [JoinTime] datetime2 NOT NULL,
    [ActivityID] nvarchar(450) NOT NULL,
    CONSTRAINT [PK_VAVRelationshipsTable] PRIMARY KEY ([RelationID]),
    CONSTRAINT [FK_VAVRelationshipsTable_VoluntaryActivities_ActivityID] FOREIGN KEY ([ActivityID]) REFERENCES [VoluntaryActivities] ([ActivityID]) ON DELETE CASCADE,
    CONSTRAINT [FK_VAVRelationshipsTable_Voluntary_VoluntaryID] FOREIGN KEY ([VoluntaryID]) REFERENCES [Voluntary] ([VoluntaryID]) ON DELETE CASCADE
);
GO

CREATE TABLE [ActivityReviews] (
    [ReviewID] nvarchar(450) NOT NULL,
    [VAAndVID] nvarchar(450) NOT NULL,
    [ReviewContent] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_ActivityReviews] PRIMARY KEY ([ReviewID]),
    CONSTRAINT [FK_ActivityReviews_VAVRelationshipsTable_VAAndVID] FOREIGN KEY ([VAAndVID]) REFERENCES [VAVRelationshipsTable] ([RelationID]) ON DELETE CASCADE
);
GO

IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'ManagerID', N'Name', N'Password') AND [object_id] = OBJECT_ID(N'[Managers]'))
    SET IDENTITY_INSERT [Managers] ON;
INSERT INTO [Managers] ([ManagerID], [Name], [Password])
VALUES (N'0b129cc5-5260-4e08-a1c0-b356152177cd', N'Test1', N'6666666');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'ManagerID', N'Name', N'Password') AND [object_id] = OBJECT_ID(N'[Managers]'))
    SET IDENTITY_INSERT [Managers] OFF;
GO

IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'OrganizationID', N'Area', N'City', N'OrganizationName', N'OrganizationPassword', N'OrganizationTelephone', N'Province') AND [object_id] = OBJECT_ID(N'[Organizations]'))
    SET IDENTITY_INSERT [Organizations] ON;
INSERT INTO [Organizations] ([OrganizationID], [Area], [City], [OrganizationName], [OrganizationPassword], [OrganizationTelephone], [Province])
VALUES (N'660c6878-1bda-4572-9eb5-ecf538f095c9', N'顺德区', N'佛山市', N'Test1', N'666666', N'135382223769', N'广东省');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'OrganizationID', N'Area', N'City', N'OrganizationName', N'OrganizationPassword', N'OrganizationTelephone', N'Province') AND [object_id] = OBJECT_ID(N'[Organizations]'))
    SET IDENTITY_INSERT [Organizations] OFF;
GO

IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'VoluntaryID', N'Area', N'City', N'Province', N'VoluntaryName', N'VoluntaryPassword', N'VoluntaryTelephone') AND [object_id] = OBJECT_ID(N'[Voluntary]'))
    SET IDENTITY_INSERT [Voluntary] ON;
INSERT INTO [Voluntary] ([VoluntaryID], [Area], [City], [Province], [VoluntaryName], [VoluntaryPassword], [VoluntaryTelephone])
VALUES (N'bce85775-2f9c-4066-b2f4-555ced8a505c', N'长清区', N'济南市', N'山东省', N'Test1', N'666666', N'15007473346');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'VoluntaryID', N'Area', N'City', N'Province', N'VoluntaryName', N'VoluntaryPassword', N'VoluntaryTelephone') AND [object_id] = OBJECT_ID(N'[Voluntary]'))
    SET IDENTITY_INSERT [Voluntary] OFF;
GO

CREATE INDEX [IX_ActivityReviews_VAAndVID] ON [ActivityReviews] ([VAAndVID]);
GO

CREATE INDEX [IX_VAVRelationshipsTable_ActivityID] ON [VAVRelationshipsTable] ([ActivityID]);
GO

CREATE INDEX [IX_VAVRelationshipsTable_VoluntaryID] ON [VAVRelationshipsTable] ([VoluntaryID]);
GO

CREATE INDEX [IX_VoluntaryActivities_ActivityTypeID] ON [VoluntaryActivities] ([ActivityTypeID]);
GO

CREATE INDEX [IX_VoluntaryActivities_OrganizationID] ON [VoluntaryActivities] ([OrganizationID]);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20230826005101_init', N'7.0.9');
GO

COMMIT;
GO

